/*
 * @Author: your name
 * @Date: 2021-04-10 13:41:30
 * @LastEditTime: 2021-04-20 18:30:46
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\reserveMod\service.go
 */
package reserveMod

import (
	"chronic/common"
	"chronic/module/userMod"
	"strconv"
)

func addOneReserve(patientId uint, input reserveDto) (*Reserve, error) {
	db := common.UseDb()
	doctorid, _ := strconv.ParseUint(input.DoctorID, 10, 64)
	resv := Reserve{PatientID: patientId, PatientName: input.PatientName, PatientNumber: input.PatientNumber, PatientTel: input.PatientTel, DoctorID: uint(doctorid), ReserveTime: input.ReserveTime}
	err := db.Save(&resv).Error
	return &resv, err
}

func updataReserveQuota(resv *Reserve) (*ReserveQuota, error) {
	db := common.UseDb()
	var resq ReserveQuota
	err := db.Where("doctor_id = ? AND any_duration = ?", resv.DoctorID, resv.ReserveTime).Find(&resq).Error
	if err != nil {
		return &resq, err
	} else {
		resq.TheDurationNum += 1
		db.Save(&resq)
		return &resq, err
	}
}

// func findBanReserveDoctors(reserveTime string) (banResqs []*ReserveQuota, err error) {
// 	reserveMax := 5
// 	db := common.UseDb()
// 	err = db.Where("any_duration = ? AND the_duration_num = ?", reserveTime, reserveMax).Find(&banResqs).Error
// 	return
// }

func findEnableDoctors(time string) ([]*ReserveQuota, error) {
	db := common.UseDb()
	var resq []*ReserveQuota
	err := db.Where("any_duration = ? AND the_duration_num < the_duration_max", time).Preload("Doctor").Find(&resq).Error
	return resq, err
}

func addReserveQuota(doctorId uint, input reserveQuotaDto) (*ReserveQuota, error) {
	db := common.UseDb()
	var resq ReserveQuota
	resq = ReserveQuota{DoctorID: doctorId, AnyDuration: input.AnyDuration, TheDurationMax: input.TheDurationMax, TheDurationNum: 0}
	err := db.Save(&resq).Error
	return &resq, err
}

func findReservationsByPatientId(patientId uint) ([]*Reserve, error) {
	db := common.UseDb()
	var resvs []*Reserve
	err := db.Where("patient_id=?", patientId).Preload("Doctor").Order("updated_at desc").Find(&resvs).Error
	return resvs, err
}

func findAllReserves() ([]*Reserve, error) {
	db := common.UseDb()
	var resvs []*Reserve
	err := db.Preload("Doctor").Order("created_at desc").Find(&resvs).Error
	return resvs, err
}

func findReservesByUserId(userId uint) ([]*Reserve, error) {
	db := common.UseDb()
	var resvs []*Reserve
	err := db.Where("patient_id=?", userId).Or("doctor_id=?", userId).Preload("Doctor").Order("updated_at desc").Find(&resvs).Error
	return resvs, err
}

func findReservesByPatient(condition string) ([]*Reserve, error) {
	db := common.UseDb()
	var resvs []*Reserve
	err := db.Where("patient_name=?", condition).Or("patient_number=?", condition).Or("patient_tel=?", condition).Or("reserve_time=?", condition).Preload("Doctor").Order("updated_at desc").Find(&resvs).Error
	return resvs, err
}

func findReservesByDoctor(condition string) ([]*Reserve, error) {
	// 1....2....
	db := common.UseDb()
	var user userMod.User
	db.Where("name=?", condition).Or("phone=?", condition).Or("doctor_hospital=?", condition).First(&user)
	resvs, err := findReservesByUserId(user.ID)
	return resvs, err
}

func deleteReserveById(id uint) error {
	db := common.UseDb()
	err := db.Where("id = ?", id).Delete(&Reserve{}).Error
	return err
}
